<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>role_operate</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <style>
        .view-form .layui-input-block {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>
<body>
<div style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称：</label>
            <div class="layui-input-block">
                <input type="text" name="ROLE_NAME" lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">选择权限：</label>
            <div class="layui-input-block auth-list">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <textarea name="ROLE_INFO" placeholder="请输入备注" autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="role" id="buttons">保存</button>
                <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    layui.use(['jquery', 'form'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery;

        //操作类型：add新增，edit修改，view查看
        var operateType = QueryUtils.GetQueryString("type");
        var id = QueryUtils.GetQueryString("id");

        var active = {
            // 初始化权限列表
            initAuth: function () {
                $.ajax({
                    url: "/shsRole/authList",
                    data: null,
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {

//                           console.log(res.data);
                            if (!!res.data) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var d = res.data[i];
                                    if (d.PARENT_ID == '0') {
                                        var str = '<fieldset class="layui-elem-field">\n' +
                                            '          <legend>\n' +
                                            '              <input type="checkbox" name="AUTH_CODE" value="' + d.AUTH_CODE + '" lay-skin="primary" lay-filter="authCheckbox" title="' + d.AUTH_NAME + '">\n' +
                                            '          </legend>\n' +
                                            '          <div class="layui-field-box auth-' + d.ID + '">\n' +
                                            '          </div>\n' +
                                            '      </fieldset>';
                                        $(".auth-list").append(str);
                                    } else {
                                        var str = '<input type="checkbox" name="AUTH_CODE" value="' + d.AUTH_CODE + '" lay-skin="primary" lay-filter="authCheckBoxChild" title="' + d.AUTH_NAME + '">';
                                        $(".auth-" + d.PARENT_ID).append(str);
                                    }
                                }
                                form.render();
                            }
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            }
        };

        active.initAuth();

        switch (operateType) {
            case "add":
                break;
            case "edit":
                $.ajax({
                    url: "/shsRole/info",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $("input[name='ROLE_NAME']").val(res.data.ROLE_NAME);
                            $("textarea[name='ROLE_INFO']").val(res.data.ROLE_INFO);

                            var auth = res.data.AUTH_CODE.split(",");
                            if (!!auth) {
                                for (var i = 0; i < auth.length; i++) {
                                    $("input[type='checkbox'][value='" + auth[i] + "']").prop("checked", true);
                                }
                            }

                            form.render();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
            case "view":
                $.ajax({
                    url: "/shsRole/info",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $("input[name='ROLE_NAME']").val(res.data.ROLE_NAME);
                            $("textarea[name='ROLE_INFO']").val(res.data.ROLE_INFO);
                            var auth = res.data.AUTH_CODE.split(",");
                            if (!!auth) {
                                for (var i = 0; i < auth.length; i++) {
                                    $("input[type='checkbox'][value='" + auth[i] + "']").prop("checked", true);
                                }
                            }

                            form.render();
                            $("input").attr("disabled","disabled");
                            $("textarea").attr("readonly","readonly");
                            $("#buttons").attr("disabled","disabled");
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }

        form.on('checkbox(authCheckbox)', function (data) {
            if (data.elem.checked) {
                $(data.elem).parent().parent().find("input[type='checkbox']").prop("checked", true);
            } else {
                $(data.elem).parent().parent().find("input[type='checkbox']").prop("checked", false);
            }
            form.render();
        });

        form.on('checkbox(authCheckBoxChild)', function (data) {
            var i = $(data.elem).parent().find("input[type='checkbox']").size();
            var j = $(data.elem).parent().find("input[type='checkbox']:checked").size();
            if (j > 0) {
                $(data.elem).parent().parent().find("legend").find("input[type='checkbox']").prop("checked", true);
            } else {
                $(data.elem).parent().parent().find("legend").find("input[type='checkbox']").prop("checked", false);
            }
            form.render();
        });

        //监听提交
        form.on('submit(role)', function (data) {
            if (operateType === "add") {
                var AUTH_CODE = '';
                $("input[name='AUTH_CODE']").each(function (index) {
                    if ($(this).is(':checked')) {
                        AUTH_CODE += $(this).val() + ",";
                    }
                });
                AUTH_CODE = AUTH_CODE.replace(/(.*)[,，]$/, '$1');
                data.field.AUTH_CODE = AUTH_CODE;
                /*layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                });*/
                layer.msg('确认保存角色吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/shsRole/save",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建商圈页面
                                    parent.reloadTable();    // 父页面表格刷新
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            } else if (operateType === "edit") {
                data.field.ID = id;
                var AUTH_CODE = '';
                $("input[name='AUTH_CODE']").each(function (index) {
                    if ($(this).is(':checked')) {
                        AUTH_CODE += $(this).val() + ",";
                    }
                });
                AUTH_CODE = AUTH_CODE.replace(/(.*)[,，]$/, '$1');
                data.field.AUTH_CODE = AUTH_CODE;
                layer.msg('确认保存角色吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/shsRole/edit",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭修改商圈页面
                                    parent.reloadTable();    // 父页面表格刷新
                                    parent.layer.msg("修改成功", {offset: '150px'});   // 父页面弹出修改成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });

    });
</script>
</body>
</html>